package com.chif.mapreduce.MapJion;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class MapJoinDriver {

    public static void main(String[] args) throws IOException, URISyntaxException, ClassNotFoundException, InterruptedException {
        //1 获取job信息
        Configuration conf = new Configuration();
        //指定hadoop运行时在本地的临时工作目录
        conf.set("hadoop.tmp.dir", "D:/tmp/mapreduce_tmp_cache");

        Job job = Job.getInstance(conf);

        //2 设置加载jar包路径
        job.setJarByClass(MapJoinDriver.class);
        job.setMapperClass(MapJoinMapper.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass( NullWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        //加载缓存数据到Task运行节点
        job.addCacheFile(new URI("file:///D:/cache/pd.txt"));
        /**
         * 如果是集群运行，需要设置HDFS路径
         * job.addCacheFile(new URI("hdfs://hadoop102:8020/cache/pd.txt"));
         */
        //Map端Join的逻辑不需要Reduce阶段，设置reduceTask数量为0
        job.setNumReduceTasks(0);

        //设置输入输出路径
        FileInputFormat.setInputPaths(job,new Path("C:\\Users\\Chef Liu\\Desktop\\Big\\hadoop\\input3"));
        FileOutputFormat.setOutputPath(job,new Path("C:\\Users\\Chef Liu\\Desktop\\Big\\hadoop\\output"));

        boolean b = job.waitForCompletion(true);
        System.exit(b?0:1);
    }
}
